package com.platform.service;

import com.platform.dao.*;
import com.platform.entity.*;
import com.platform.utils.Query;
import com.platform.validator.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service
public class ApiUserService {
    @Autowired
    private ApiUserMapper apiUserMapper;
    @Autowired
    private ApiFollowMapper apiFollowMapper;
    @Autowired
    private ApiResourceMapper apiResourceMapper;
    @Autowired
    private ApiOrderMapper apiOrderMapper;
    @Autowired
    private ApiUserLabelMapper userLabelMapper;
    @Autowired
    private ApiLabelMapper labelMapper;

    public UserVo queryObject(Long userId) {
        return apiUserMapper.queryObject(userId);
    }

    public UserVo queryByOpenId(String openId) {
        return apiUserMapper.queryByOpenId(openId);
    }

    public List<UserVo> queryList(Map<String, Object> map) {
        return apiUserMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiUserMapper.queryTotal(map);
    }

    public void save(String mobile, String password) {
        UserVo user = new UserVo();
        user.setMobile(mobile);
        user.setUsername(mobile);
        user.setPassword(DigestUtils.sha256Hex(password));
        user.setRegister_time(new Date());
        apiUserMapper.save(user);
    }

    public void save(UserVo userVo) {
        apiUserMapper.save(userVo);
    }

    public void update(UserVo user) {
        apiUserMapper.update(user);
    }

    public void delete(Long userId) {
        apiUserMapper.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        apiUserMapper.deleteBatch(userIds);
    }

    public UserVo queryByMobile(String mobile) {
        return apiUserMapper.queryByMobile(mobile);
    }

    public long login(String mobile) {
        UserVo user = queryByMobile(mobile);
        Assert.isNull(user, "手机号错误");
        return user.getId();
    }


    public void recharge(UserVo loginUser, String flat, BigDecimal amount) {
        if (flat.equals("add")) {
            loginUser.setAvailable_amount(loginUser.getAvailable_amount().add(amount));
            loginUser.setWithdraw_amount(loginUser.getWithdraw_amount().add(amount));
            loginUser.setPresent_amount(loginUser.getPresent_amount().add(amount));
        }
        if (flat.equals("del")) {
            loginUser.setAvailable_amount(loginUser.getAvailable_amount().subtract(amount));

            if (loginUser.getPresent_amount().compareTo(amount) >= 0) {
                loginUser.setPresent_amount(loginUser.getPresent_amount().subtract(amount));
            } else {
                loginUser.setPresent_amount(new BigDecimal(0));
            }
        }
        apiUserMapper.update(loginUser);
    }

    public UserInfo toUserInfo(Long userId, UserVo loginUser) {
        UserVo userVo = apiUserMapper.queryObject(userId);

        if (userVo != null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userVo.getId());
            userInfo.setAvailableAmount(userVo.getAvailable_amount());
            userInfo.setWithdrawAmount(userVo.getWithdraw_amount());
            userInfo.setAge(userVo.getAge());
            userInfo.setAvatar(userVo.getAvatar());
            userInfo.setNickName(userVo.getNickname());
            userInfo.setIdentity(userVo.getIdentity());
            userInfo.setIntroduction(userVo.getIntroduction());
            userInfo.setApplicationStatus(userVo.getApplication_status());
            userInfo.setOnlineStatus(userVo.getOnline_status());
            userInfo.setPoster(userVo.getCover_url());
            userInfo.setLive(userVo.getLive());
            userInfo.setLivePrice(userVo.getLive_price());
            userInfo.setBirthday(userVo.getBirthday());
            userInfo.setWake(userVo.getWake());
            userInfo.setWakePrice(userVo.getWake_price());
            userInfo.setEmotion(userVo.getEmotion());
            userInfo.setEmotionPrice(userVo.getEmotion_price());
            userInfo.setVoiceUrl(userVo.getVoice_url());
            userInfo.setGuide(userVo.getGuide());
            userInfo.setPresentAmount(userVo.getPresent_amount());
            userInfo.setMobile(userVo.getMobile());
            userInfo.setGuidePrice(userVo.getGuide_price());
            userInfo.setWeight(userVo.getWeight());
            userInfo.setHeight(userVo.getHeight());
            userInfo.setConstellation(userVo.getConstellation());
            userInfo.setCityName(userVo.getCity_name());
            userInfo.setReception(userVo.getReception());
            userInfo.setGender(userVo.getGender());
            Map<String, Object> param = new HashMap<>();
            param.put("mouldId", userVo.getId());
            param.put("mouldName", "userVo");
            param.put("type", 1);

            /**
             * 海报
             */
            List<ApiResourceVo> resourceVos = apiResourceMapper.queryList(param);
            if (resourceVos.size() > 0) {
                Integer rand = RandomUtils.nextInt(0, resourceVos.size());
                userVo.setCover_url(resourceVos.get(rand).getUrl());
                apiUserMapper.update(userVo);
                userInfo.setPics(apiResourceMapper.queryList(param));
            }

            /**
             * 是否有标签
             */

            Map<String, Object> myLables = new HashMap();
            myLables.put("page", 1);
            myLables.put("limit", 10);
            myLables.put("zbUserId", userId);
            myLables.put("mould_name", "userVo");
            List<Map<String, Object>> myLabels = userLabelMapper.selectMap(myLables);

            if (myLabels.size() > 0) {
                List<ApiLabelVo> lableList = new ArrayList<>();

                for (Map<String, Object> map : myLabels) {
                    Map<String, Object> param_la = new HashMap();

                    param_la.put("name", map.get("name"));

                    List<ApiLabelVo> labelVos = labelMapper.queryList(param_la);
                    if (labelVos != null && labelVos.size() > 0) {
                        lableList.add(labelVos.get(0));
                    }
                }
                userInfo.setLabelVos(lableList);
            }


            if (loginUser != null && !userId.equals(loginUser.getId())) {
                /**
                 * 是否预约 1 是 2 否
                 */
                Map params = new HashMap();
                params.put("status", 1);
                params.put("zbUserId", userId);
                params.put("cmUserId", loginUser.getId());
                Integer countU = apiOrderMapper.queryTotal(params);
                if (countU > 0) {
                    userInfo.setIsOrder(1);
                } else {
                    userInfo.setIsOrder(2);
                }

                /**
                 * 是否关注  1 是 2 否
                 */
                params = new HashMap();
                params.put("zbUserId", userId);
                params.put("cmUserId", loginUser.getId());
                Integer countF = apiFollowMapper.queryTotal(params);
                if (countF > 0) {
                    userInfo.setIsFollow(1);
                } else {
                    userInfo.setIsFollow(2);
                }

            }
            return userInfo;
        }

        return null;
    }

}
